package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xmy.cultivate.entity.Course;
import com.xmy.cultivate.entity.Grade;
import com.xmy.cultivate.entity.OrderDetail;
import com.xmy.cultivate.entity.StudentType;
import com.xmy.cultivate.exception.CommonException;
import com.xmy.cultivate.mapper.OrderDetailMapper;
import com.xmy.cultivate.mapper.StudentTypeMapper;
import com.xmy.cultivate.service.IStudentTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmy.cultivate.util.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 是否新生 服务实现类
 * </p>
 *
 * @author min
 * @since 2022-11-28
 */
@Service
public class StudentTypeServiceImpl extends ServiceImpl<StudentTypeMapper, StudentType> implements IStudentTypeService {

    @Autowired
    @Lazy
    StudentTypeMapper studentTypeMapper;

    @Autowired
    @Lazy
    OrderDetailMapper orderDetailMapper;

    public boolean addData(Long school_id, Long student_id,Long subjects_id,Integer type){
        QueryWrapper<StudentType> studentTypeQueryWrapper = new QueryWrapper<>();
        studentTypeQueryWrapper.eq("school_id",school_id);
        studentTypeQueryWrapper.eq("student_id",student_id);
        studentTypeQueryWrapper.eq("subjects_id",subjects_id);
        studentTypeQueryWrapper.last("limit 1");
        StudentType studentType = studentTypeMapper.selectOne(studentTypeQueryWrapper);

        if(studentType == null){
            StudentType studentTypeAdd = new StudentType();
            studentTypeAdd.setSchoolId(school_id);
            studentTypeAdd.setStudentId(student_id);
            studentTypeAdd.setSubjectsId(subjects_id);
            studentTypeAdd.setType(type);
            studentTypeMapper.insert(studentTypeAdd);
        }
        return true;

    }

    public boolean isNew(Long school_id, Long student_id,Long subjects_id){
        QueryWrapper<StudentType> studentTypeQueryWrapper = new QueryWrapper<>();
        studentTypeQueryWrapper.eq("school_id",school_id);
        studentTypeQueryWrapper.eq("student_id",student_id);
        studentTypeQueryWrapper.eq("subjects_id",subjects_id);
        StudentType studentType = studentTypeMapper.selectOne(studentTypeQueryWrapper);
        boolean rs = false;
        if(studentType != null){
            Integer type = studentType.getType();
            if(type == 1){
                rs = true;
            }
        }
        return rs;
    }

    /**
     *
     * @param school_id
     * @param student_id
     * @param subjects_id
     * @param type
     * @return
     */
    public boolean seType(Long school_id, Long student_id,Long subjects_id,Integer type){

        QueryWrapper<OrderDetail> orderDetailQueryWrapper = new QueryWrapper<>();
        orderDetailQueryWrapper.eq("school_id",school_id);
        orderDetailQueryWrapper.eq("student_id",student_id);
        orderDetailQueryWrapper.eq("subjects_id",subjects_id);

        /*Long count = orderDetailMapper.selectCount(orderDetailQueryWrapper);
        if(count == 0){
            throw new CommonException(ResultCode.NOTDATA);
        }*/

        QueryWrapper<StudentType> studentTypeQueryWrapper = new QueryWrapper<>();
        studentTypeQueryWrapper.eq("school_id",school_id);
        studentTypeQueryWrapper.eq("student_id",student_id);
        studentTypeQueryWrapper.eq("subjects_id",subjects_id);
        StudentType studentType = studentTypeMapper.selectOne(studentTypeQueryWrapper);
        //boolean rs = false;
        if(studentType != null){
            studentType.setType(type);
            studentTypeMapper.updateById(studentType);
        }else {
            StudentType studentTypeAdd = new StudentType();
            studentTypeAdd.setSchoolId(school_id);
            studentTypeAdd.setStudentId(student_id);
            studentTypeAdd.setSubjectsId(subjects_id);
            studentTypeAdd.setType(type);
            studentTypeMapper.insert(studentTypeAdd);
        }
        return true;
    }

    public boolean switchType(Long school_id, Long student_id,Long subjects_id){
        QueryWrapper<StudentType> studentTypeQueryWrapper = new QueryWrapper<>();
        studentTypeQueryWrapper.eq("school_id",school_id);
        studentTypeQueryWrapper.eq("student_id",student_id);
        studentTypeQueryWrapper.eq("subjects_id",subjects_id);
        StudentType studentType = studentTypeMapper.selectOne(studentTypeQueryWrapper);
        boolean rs = false;
        if(studentType != null){
            Integer type = studentType.getType();
            if(type == 1){
                studentType.setType(2);
                studentTypeMapper.updateById(studentType);
            }else {
                studentType.setType(1);
                studentTypeMapper.updateById(studentType);
            }
            rs = true;
        }
        return rs;
    }

}
